﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Windows.Input;
using VisaCRM.Modules.Admin.ViewModels.Helper;
using VisaCRM.Modules.Admin.Model;
using VisaCRM.Modules.Admin.Gateway;
using VisaCRM.Common.Helper;
using Microsoft.Practices.Prism.Mvvm;

namespace VisaCRM.Modules.Admin.ViewModels
{
    // <summary>
    /// ViewModel of an list of Qualification
    /// </summary>
    class QualificationViewModel:BindableBase
    {
        /// <summary>
        /// initialises all collection used
        /// </summary>
       public ObservableCollection<Qualification> allQualification;

       /// <summary>
       /// initialise gateway used
       /// </summary>
        QualificationGateway qualificationGateway = new QualificationGateway();

        private WebsiteDecideInfo websiteDecideInfo;

        public ICommand AddNewCommand { get; private set; }
        public ICommand SaveCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }
        public ICommand DeleteCommand { get; private set; }
        public ICommand EditCommand { get; private set; }
        public ICommand CloseCommand { get; private set; }

        ///summary
        ///initialises a new instance of QualificationViewModel class
        ///summary
        public QualificationViewModel(ObservableCollection<Qualification> allqualification)
       {
           currentQualification = new Qualification();
           
           if (allqualification == null || allqualification.Count == 0)
           {

               AllQualification = qualificationGateway.GetAllQualification();
           }
           else
           {
               this.AllQualification = allqualification;
           }

           /// Re-act to any changes from outside this ViewModel
           this.AllQualification.CollectionChanged += (sender, e) =>
           {
               if (e.OldItems != null && e.OldItems.Contains(this.CurrentQualification))
               {
                   this.CurrentQualification = null;
               }
           };

          this.websiteDecideInfo = new WebsiteDecideInfo();
          this.WebsiteDecide = WebsiteDecide.Nothing;

           this.SaveCommand = new DelegateCommand((o) => this.Save());
           this.AddNewCommand = new DelegateCommand((o) => this.AddNew());
           this.CancelCommand = new DelegateCommand((o) => this.Cancel());
           this.DeleteCommand = new DelegateCommand((o) => this.Delete());
           this.EditCommand = new DelegateCommand((o) => this.Edit());
           this.CloseCommand = new DelegateCommand((o) => this.Close());
       }

        public WebsiteDecideInfo WebsiteDecideInfo
        {
            get { return this.websiteDecideInfo; }
            set
            {
                SetProperty(ref this.websiteDecideInfo, value);
                this.OnPropertyChanged(() => this.WebsiteDecide);
            }
        }

        public WebsiteDecide WebsiteDecide
        {
            get 
            { 
                return this.WebsiteDecideInfo.WebsiteDecide; 
            }
            set
            {
                this.WebsiteDecideInfo.WebsiteDecide = value;
                if (this.WebsiteDecide == WebsiteDecide.No)
                {
                    this.CurrentQualification.QualificationDegree = 0;
                }
                if (this.WebsiteDecide == WebsiteDecide.Yes)
                {
                    this.CurrentQualification.QualificationDegree = 1;
                }
                this.OnPropertyChanged("WebsiteDecide");
            }
        }

       private void Close()
       {
           throw new NotImplementedException();
       }

       private void Edit()
       {
           if (this.CurrentQualification != null)
           {
               Qualification qualification = new Qualification() { QualificationName = this.CurrentQualificationName, QualificationID = this.CurrentQualification.QualificationID, QualificationDegree = this.CurrentQualification.QualificationDegree };
               qualificationGateway.EditQualificationToDatabase(this.CurrentQualificationName,this.currentQualification.QualificationDegree, this.currentQualification.QualificationID);
               this.AllQualification = qualificationGateway.GetAllQualification();
               this.CurrentQualification = qualification;
               System.Windows.Forms.MessageBox.Show("The Qualification details is editted sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please select Qualification details to be edited");
           }
       }

       private void Delete()
       {
           if (this.CurrentQualification != null)
           {
               qualificationGateway.DeleteQualificationFromDatabase(this.currentQualification.QualificationID);
               AllQualification.Remove(this.CurrentQualification);
               AddNew();
               System.Windows.Forms.MessageBox.Show("The where Qualification is deleted sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please selsect Qualification details to be deleted");
           }
       }

       private void Cancel()
       {
           this.currentQualification = null;
       }

       private void AddNew()
       {
           this.CurrentQualificationName = null;
           this.WebsiteDecide = WebsiteDecide.Nothing;
       }

       private void Save()
       {
           if (this.CurrentQualificationName != null)
           {
               Qualification qualification = new Qualification() { QualificationName = this.CurrentQualificationName };
               bool flag = false;
               if (string.IsNullOrEmpty(qualification.QualificationName))
                   return;
               if (string.IsNullOrEmpty(qualification.QualificationDegree.ToString()))
                   return;
               foreach (Qualification q in this.AllQualification)
               {
                   if (q.QualificationName == qualification.QualificationName)
                   {
                       flag = true;
                       break;
                   }
               }
               if (flag)
               {
                   System.Windows.Forms.MessageBox.Show("The Qualification details are already listed");
                   return;
               }
               qualificationGateway.SaveNewQualificationToDatabase(this.CurrentQualificationName,this.currentQualification.QualificationDegree);
               AddNew();
               this.AllQualification = qualificationGateway.GetAllQualification();
               System.Windows.Forms.MessageBox.Show("The Qualification details are saved sucessfully");
           }
           else
           {
               System.Windows.Forms.MessageBox.Show("Please fill Qualification and Is Degree Eqvivalent button details");
           }
       }

       /// <summary>
       /// The Qualificationr currently selected in the workspace
       /// </summary>
       private Qualification currentQualification;

       /// <summary>
       /// Gets or sets the Qualification currently selected in the workspace
       /// </summary>
       public Qualification CurrentQualification
       {
           get
           {
               return this.currentQualification;
           }

           set
           {
               this.currentQualification = value;
               if (this.CurrentQualification != null)
               {
                   this.CurrentQualificationName = this.CurrentQualification.QualificationName;
                   if (this.CurrentQualification.QualificationDegree == 0)
                   {
                       this.WebsiteDecide = WebsiteDecide.No;
                   }
                   if (this.CurrentQualification.QualificationDegree == 1)
                   {
                       this.WebsiteDecide = WebsiteDecide.Yes;
                   }
               }
               this.OnPropertyChanged("CurrentQualification");
           }
       }

       /// <summary>
       /// the QualificationName currently selected in the workspace
       /// </summary>
       private string currentQualificationName;
       /// <summary>
       /// Gets or sets the QualificationName currently selected in the workspace
       /// </summary>
       public string CurrentQualificationName
       {
           get
           {
               return this.currentQualificationName;
           }

           set
           {
               this.currentQualificationName = value;
               this.OnPropertyChanged("CurrentQualificationName");
           }
       }

       /// <summary>
       /// Gets or sets the Qualification collection
       /// </summary>
       public ObservableCollection<Qualification> AllQualification
       {
           get
           {
               return this.allQualification;
           }

           set
           {
               this.allQualification = value;
               this.OnPropertyChanged("AllQualification");
           }
       }
    }
}
